package com.aiyige.page.my.localVideo;

import com.aiyige.base.db.DBHelper;
import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


/**
 * （选择视频那个界面是扫描本地数据，然后选择了之后，保存在这里。（本地视频））
 * @author Administrator
 *
 */
public class LocalMovieDao {
	private Dao<LocalMovieInfo, Integer> dao;

	public LocalMovieDao() {
		try {
			dao = DBHelper.getInstance().getDao(LocalMovieInfo.class);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 *  添加本地视频
	 * @param info
	 */
	public void addLocalMovie(LocalMovieInfo info) {
		try {
			if (!isHasAdd(info.path)) {
				dao.create(info);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 是否已添加
	 * @param path 视频本地地址
	 * @return
	 */
	public boolean isHasAdd(String path) {
		try {
			List<LocalMovieInfo> list = dao.queryBuilder().where().eq("path", path).query();
			if (!list.isEmpty()) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 删除本地视频
	 * @param info
	 */
	public void removeLocalMovie(LocalMovieInfo info) {
		try {
			dao.delete(info);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 查询本地视频
	 * @return
	 */
	public List<LocalMovieInfo> queryLocalMovies() {
		List<LocalMovieInfo> list = new ArrayList<LocalMovieInfo>();
		try {
			list.addAll(dao.queryBuilder().orderBy("dateTime", false).query());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 *  获取本地视频总数
	 * @return
	 */
	public int getLocalCount() {
		int count = 0;
		try {
			count = Long.valueOf(dao.countOf()).intValue();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}
}
